package com.zpark.wxshop.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpark.wxshop.Vo.OrderInfoVo;
import com.zpark.wxshop.service.ICartService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.wxshop.Utils.Result;

import com.zpark.wxshop.service.IOrderService;
import com.zpark.wxshop.entity.Order;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PJone
 * @since 2023-09-07
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    @Resource
    private RedisTemplate redisTemplate;


    /**
    * 新增与修改
    */
    @PostMapping
    public Result save(@RequestBody Order order) {
        orderService.saveOrUpdate(order);
        return Result.success();
    }

    /**
    * 根据id删除
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        orderService.removeById(id);
        return Result.success();
    }

    /**
    * 批量删除
    */
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        orderService.removeByIds(ids);
        return Result.success();
    }


    /**
    * 查询所有
    */
    @GetMapping
    public Result findAll() {
        return Result.success(orderService.list());
    }

    /**
    * 根据id查询单个
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(orderService.getById(id));
    }

    /**
    * 分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(orderService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 支付订单之后
     */
    @RequestMapping("/saveOrder/{uid}")
    public Result saveOrder(@PathVariable("uid") Integer uid, @RequestParam("orderid") String orderid){
        Order order = new Order();
        order.setStart(2);
        order.setUid(uid);
        order.setOrderid(orderid);
        orderService.save(order);
        orderService.SaveOrders(orderid);
        redisTemplate.delete("Card:checkProducts:" + orderid);
        return Result.success(order);
    }

    /**
     * 查询该用户订单信息
     */
    @RequestMapping("/findOrders/{uid}")
    public Result UserOrders(@PathVariable("uid") Integer uid, @RequestParam(value = "start", required = false) Integer start, @RequestParam(value = "orderid", required = false) String orderid){
        List<Order> orders = new ArrayList<>();
        List<OrderInfoVo> list = new ArrayList<>();
        if (start == 0){
            orders = orderService.list(new QueryWrapper<Order>().eq("uid", uid));
            for (Order order : orders){
                list.add(orderService.FindOrderByOrderid(order.getOrderid()));
            }
        }else if (start == 1){
            list.add((OrderInfoVo) redisTemplate.opsForValue().get("Card:checkProducts:" + orderid));
        } else {
            orders = orderService.list(new QueryWrapper<Order>().eq("uid", uid).eq("start", start));
            for (Order order : orders){
                list.add(orderService.FindOrderByOrderid(order.getOrderid()));
            }
        }
        return Result.success(list);
    }

}

